package com.ruoyi.erp.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.OpportunityMapper;
import com.ruoyi.erp.domain.Opportunity;
import com.ruoyi.erp.service.IOpportunityService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商机Service业务层处理
 * 
 * @author sauger
 * @date 2022-04-01
 */
@Service
public class OpportunityServiceImpl implements IOpportunityService 
{
    @Autowired
    private OpportunityMapper opportunityMapper;

    /**
     * 查询商机
     * 
     * @param id 商机主键
     * @return 商机
     */
    @Override
    public Opportunity selectOpportunityById(Integer id)
    {
        return opportunityMapper.selectOpportunityById(id);
    }

    /**
     * 查询商机列表
     * 
     * @param opportunity 商机
     * @return 商机
     */
    @Override
    public List<Opportunity> selectOpportunityList(Opportunity opportunity)
    {
        return opportunityMapper.selectOpportunityList(opportunity);
    }

    /**
     * 新增商机
     * 
     * @param opportunity 商机
     * @return 结果
     */
    @Override
    public int insertOpportunity(Opportunity opportunity)
    {
        int number = opportunityMapper.selectExistsCountByDate(opportunity.getRegisterTime()) + 1;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        opportunity.setSn("XS" + format.format(opportunity.getRegisterTime()) + number);
        return opportunityMapper.insertOpportunity(opportunity);
    }

    /**
     * 修改商机
     * 
     * @param opportunity 商机
     * @return 结果
     */
    @Override
    public int updateOpportunity(Opportunity opportunity)
    {
        opportunity.setUpdateTime(DateUtils.getNowDate());
        return opportunityMapper.updateOpportunity(opportunity);
    }

    /**
     * 批量删除商机
     * 
     * @param ids 需要删除的商机主键
     * @return 结果
     */
    @Override
    public int deleteOpportunityByIds(String ids)
    {
        return opportunityMapper.deleteOpportunityByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商机信息
     * 
     * @param id 商机主键
     * @return 结果
     */
    @Override
    public int deleteOpportunityById(Integer id)
    {
        return opportunityMapper.deleteOpportunityById(id);
    }
}
